<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../inc.jsp"></jsp:include>
<meta http-equiv="X-UA-Compatible" content="edge" />
<title>角色管理</title>
<script type="text/javascript">
	var dataGrid;
	$(function() {
		selectRoles();
	});
	//加载数据列表
	function selectRoles(){
		if('${resCode}'==='1'){
			if('${ROLE_MANAGER}'){
		dataGrid = $('#dataGrid').datagrid(
						{
							url : '${ctx}' + '/role/roleListData',
							striped : true,
							fitColumns:true,
							rownumbers : true,
							pagination : true,
							singleSelect : true,
							idField : 'roId',
							sortName : 'roId',
							sortOrder : 'asc',
							pageSize : 10,
							pageList : [ 10, 20, 30, 40, 50, 100, 200, 300,
									400, 500 ],
							frozenColumns : [ [
									{
										width : '20%',
										title : '名称',
										field : 'toName',
										sortable : true
									},
									{
										width : '20%',
										title : '创建时间',
										field : 'strData'
									},
									{
										width : '20%',
										title : '类型',
										field : 'value'
									},
									{
										width : '20%',
										title : '描述',
										field : 'notes'
									},
									{
										field : 'action',
										title : '操作',
										width : '19%',
										formatter : function(value, row, index) {
											
											var str = '&nbsp;';
											if('${ROLE_GRANT}'){
											str += $.formatString('<a href="javascript:void(0)" style="text-decoration:none;" onclick="grantFun(\'{0}\');" >授权</a>',row.roId);
											   if('${ROLE_EDIT}' || '${ROLE_DELETE}'){
												   str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
											    }
											}
											if('${ROLE_EDIT}'){
											str += $.formatString('<a href="javascript:void(0)" style="text-decoration:none;" onclick="editFun(\'{0}\');" >编辑</a>',row.roId);
												if('${ROLE_DELETE}'){
													str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
												}
											}
											if('${ROLE_DELETE}'){
											str += $
													.formatString(
															'<a href="javascript:void(0)" style="text-decoration:none;" onclick="deleteFun(\'{0}\');" >删除</a>',
															row.roId);
											}
											return str;
										}
									} ] ],
							toolbar : '#toolbar'
						});
			}else{
				parent.$.messager.confirm('询问', '当前登录用户没有操作此功能的权限，重新登录！！',
						function(b) {
							if (b) {
								parent.window.location.href = '${ctx}/login';
							}
						});
			}
		}else if('${resCode}'==='0'){
			parent.$.messager.confirm('询问', '您还未登录或登录超时！请重新登录？', function(b) {
				if (b) {
					parent.window.location.href='${ctx}/login';
				}
			});
		}		
	} 
	
	function addFun() {
		//update   by  xujun  2017-07-29
		$("#addRole").dialog({
        title: '添加',
        href: '${ctx}/role/addRole',
        width:'400',
	    height:'250',
        modal: true,
        onClose:function(){
        	location.reload();
        } 
    	});
		
	} 

	function deleteFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前角色？', function(b) {
			if (b) {
				//update   by   XUJUN2017-07-29   
				jQuery.ajax({
					url: '${ctx}/role/deleteRole',
					type: "POST",
					async: "false",
					timeout: 30000,
					data: {"id":id},
					success: function(ret) {
						if(ret.state==0){
							parent.$.messager.show({
								showType:'fade',
							    showSpeed:600,
							    width:300,
							    height:126,
							    msg:'删除成功',
							    title:'提示',
							    timeout:1000,
							    style:{
									right:'',
									top:'200px',
									bottom:'',
								}
							    });
							selectRoles();
						}else if(ret.state==-1){
							parent.$.messager.alert('提示',"此角色下还有用户存在，不能删除", 'info');
							selectRoles();
						}else{
							parent.$.messager.alert('提示',"删除失败", 'info');
							selectRoles();
						}
					},
					error: function() {
						parent.$.messager.alert('提示',"数据异常，删除失败！", 'error');
					}
				});	
			}
		});
	}

	function editFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		
			$("#editRole").dialog({
       	 	title: '编辑',
        	href: '${ctx}/role/editPage?id=' + id,
        	width:'400',
	    	height:'250',
        	modal: true,
        	onClose:function(){
        		location.reload();
        		} 
    		});
	}

	function viewFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}

		parent.$.modalDialog({
			title : '詳情',
			width : 200,
			height : 200,
			href : '${ctx}/demo/viewPage?id=' + id
		});
	}
	
	function grantFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '授权',
			width : 400,
			height : 500,
			href : '${ctx}/role/grantPage?rId='+id,
			buttons : [ {
				text : '授权',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#roleGrantForm');
					f.submit();
				}
			} ]
		});
	}
	
	function add_close(){
		$('#addRole').dialog('close');
	}
	function edit_close(){
		$('#editRole').dialog('close');
	}
</script>
<link rel="stylesheet" href="${ctx}/assets/css/img.css" type="text/css">
</head>
<style type="text/css">
.datagrid-body {
    overflow: hidden;
}
</style>
<body class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',fit:true,border:false">
		<table id="dataGrid" data-options="fit:true,border:false"></table>
	</div>
	<div id="toolbar" style="display: none;">

<c:if test="${ROLE_ADD}">
		<a onclick="addFun();" href="javascript:void(0);"
			class="easyui-linkbutton"
			data-options="plain:true"><img src="${ctx}/style/images/add.png" class="imgPos"/><span>&nbsp;添加</span></a>
</c:if>  

	</div>
	<div id="addRole"></div>
	<div id="editRole"></div>
</body>
</html>